package com.webwork.lims.system.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.lims.common.dao.impl.SimpleDao;
import com.webwork.lims.system.dao.MenuDao;
import com.webwork.lims.system.dataobject.Menu;
import com.webwork.lims.util.ColumnLimit;
import com.webwork.lims.util.DBSimpleSQL;
import com.webwork.lims.util.StrManager;

@Repository
public class MenuDaoImpl extends SimpleDao implements MenuDao {

	public MenuDaoImpl() {
		super();
		this.dbTable = "t_menu";
		dbColumns.clear();
		dbColumns.put("menuCode", "MENU_CODE");
		dbColumns.put("menuName", "MENU_NAME");
		dbColumns.put("menuUrl", "MENU_URL");
		dbColumns.put("menuOrder", "MENU_ORDER");
		dbColumns.put("parentCode", "PARENT_CODE");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof Menu))
			return new ColumnLimit();
		Menu o = (Menu) object;
		ColumnLimit ret = new ColumnLimit();
		if (!StrManager.isEmpty(o.getMenuCode())) {
			ret.add(dbColumns.get("menuCode") + " = ");
		}
		if (!StrManager.isEmpty(o.getMenuName())) {
			ret.add(dbColumns.get("menuName") + " like ");
		}
		if (!StrManager.isEmpty(o.getParentCode())) {
			ret.add(dbColumns.get("parentCode") + " = ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		Menu o = (Menu) object;
		List<Object> ret = new ArrayList<Object>();
		if (!StrManager.isEmpty(o.getMenuCode())) {
			ret.add(o.getMenuCode());
		}
		if (!StrManager.isEmpty(o.getMenuName())) {
			ret.add("%" + o.getMenuName() + "%");
		}
		if (!StrManager.isEmpty(o.getParentCode())) {
			ret.add(o.getParentCode());
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		Menu o = (Menu) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getMenuCode());
		objs.add(o.getMenuName());
		objs.add(o.getMenuUrl());
		objs.add(o.getMenuOrder());
		objs.add(o.getParentCode());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		Menu ret = new Menu();
		ret.setMenuCode((String) map.get(dbColumns.get("menuCode")));
		ret.setMenuName((String) map.get(dbColumns.get("menuName")));
		ret.setMenuUrl((String) map.get(dbColumns.get("menuUrl")));
		ret.setMenuOrder((Integer) map.get(dbColumns.get("menuOrder")));
		ret.setParentCode((String) map.get(dbColumns.get("parentCode")));
		return ret;
	}
	
	@Override
	public void insert(Menu o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public void update(Menu o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("menuCode") + " = ");
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(o.getMenuCode());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public void delete(String menuCode) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("menuCode") + " = ");
		String sql = DBSimpleSQL.del(dbTable, limit);
		this.getJdbcTemplate().update(sql, menuCode);
	}

	@Override
	public List<Menu> query(Menu o, int page, int size) {
		List<Menu> ret = new ArrayList<Menu>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		int limIndex = sql.indexOf("LIMIT");
		if (limIndex >= 0) {
			StringBuffer stb = new StringBuffer(sql.substring(0, limIndex));
			stb.append(" ORDER BY ");
			stb.append(dbColumns.get("menuOrder"));
			stb.append(" " + sql.substring(limIndex));
			sql = stb.toString();
		} else {
			sql += " ORDER BY " + dbColumns.get("menuOrder") + " ";
		}
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((Menu) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(Menu o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}
	
}
